import com.vk.api.sdk.client.actors.UserActor
import com.vk.api.sdk.client.{Lang, VkApiClient}
import com.vk.api.sdk.httpclient.HttpTransportClient
import model.configs.VkConfig
import zio.ZIO

object VkApiUtil {
  private val transportClient = new HttpTransportClient()
  private val vk              = new VkApiClient(transportClient)

  private def actor(id: Long, token: String) = new UserActor(id, token)

  def fetchUserInfo(userId: String, settings: VkConfig): ZIO[Any, Throwable, String] =
    ZIO.attemptBlocking(
      vk
        .users()
        .get(actor(settings.GroupId, settings.token))
        .userIds(userId)
        .lang(Lang.RU)
        .executeAsString()
    )

  def getServerAnswerCode(settings: VkConfig): ZIO[Any, Throwable, String] =
    ZIO.attemptBlocking(
      vk.groups()
        .getCallbackConfirmationCode(actor(settings.AccountId, settings.token))
        .groupId(settings.GroupId)
        .execute()
        .getCode
    )
}
